package com.scalar.chiptrack.adaptor.utac;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.TestLot;

import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.NewAdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * User: ChandraSekhar
 * Date: May 16, 2007
 * Time: 5:39:30 PM
 */
public class UTACAssemblyDataParser extends JobHandler implements AdaptorConstants
{
    private static Logger m_parserLogger    = null;
    private String m_dataOutputDir          = null;
    private HashMap m_assyWipDataMap        = new HashMap();
    private HashMap m_ftDataMap           = new HashMap();
    private HashMap m_dieBankDataMap        = new HashMap();

    public UTACAssemblyDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_UTAC, WIP_TYPE_ASSEMBLY );
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_UTAC );
            AssemblyAdaptorDAO.setLogger( m_parserLogger );   // All the DAOs and Parser should be logging into same file
            TestAdaptorDAO.setLogger( m_parserLogger);
            InventoryLotDAO.setLogger( m_parserLogger );
            AdaptorUtils.setLogger( m_parserLogger);
        }
        catch ( ScalarException se )
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading UTAC ASSEMBLY WIP output directory details from database, Exception: " + se );
            throw se;
        }
    }
    //Main method
    public static void main( String args[] ) throws Exception
    {
        new UTACAssemblyDataParser().execute();
    }

    //Execute Method
    public void execute() throws Exception
    {
        AdaptorUtils.printHeader(VENDOR_NAME_UTAC, CONSTANT_PARSER);

        File doneDestDir = new File( m_dataOutputDir + "/done" );

        File wipDataoutDir = new File( m_dataOutputDir );
       
        File[] fileList = wipDataoutDir.listFiles();

        if ( ( fileList == null ) || ( fileList.length <= 3 ) )
        {
            log( "No file found in directory: " + wipDataoutDir );
            throw new ScalarException( "No file found in directory: " + wipDataoutDir + "----\n\n" );
        }

        DBTransaction dbTransaction = null;
        ArrayList m_utacWipList     = null;
        ArrayList successFileList   = new ArrayList();
        boolean success             = false;

        try
        {
            for ( int i = 0; i < fileList.length; i++ )
            {
                if ( fileList[i].isDirectory() == true )
                {
                    continue;
                }

                if ( ( fileList[i].getName() != null ) && ( fileList[i].getName().trim().startsWith( UTACAdaptor.FILE_NAME_WIP_STATUS ) ) )
                {

                    m_utacWipList = parseAssemblyWIPReport( fileList[i].getAbsolutePath() );
                    if ( m_utacWipList != null )
                    {
                        getAllAssemblyWipLotDetails( m_utacWipList );
                    }
                    successFileList.add( fileList[i] );
                }
            }
        }
        catch ( Exception e )
        {
            success = false;
            log( "ERROR!! ERROR!! ERROR!! While parsing and storing File " + e );
            e.printStackTrace();
            throw e;
        }
        // to save the Die Bank lot tables
        try
        {
            if ( m_dieBankDataMap.isEmpty() == false )
            {
                log(" Updating Die Bank Lot Information");
                InventoryLotDAO.saveInventoryLot( dbTransaction/*null*/, m_dieBankDataMap.values().iterator(), INVENTORY_TYPE_DIE_BANK, VENDOR_NAME_UTAC );
                success = true;
            }
       }
       catch (ScalarException se)
       {
           success = false;
           se.printStackTrace();
           log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
           throw se;
       }

        // to save the Assembly lot tables
        try
        {
            if ( m_assyWipDataMap.isEmpty() == false )
            {
                log(" Updating Assembly Lot Information");
                AssemblyAdaptorDAO.saveAssemblyLot( dbTransaction/*null*/, m_assyWipDataMap.values().iterator(), WIP_TYPE_ASSEMBLY, VENDOR_NAME_UTAC );
                success = true;
            }
       }
       catch (ScalarException se)
       {
           success = false;
           se.printStackTrace();
           log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
           throw se;
       }

       /*Updating the Test Lot Information*/
       try
       {
           // Save Test Information
           if( m_ftDataMap.isEmpty() == false )
           {
               ArrayList finalTestList = new ArrayList();
               finalTestList.addAll( m_ftDataMap.values() );

               log(" Updating Test Lot Information");
               TestAdaptorDAO.saveTestLot( dbTransaction/*null*/, m_ftDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_UTAC );
               success = true;
           }
       }
       catch (ScalarException se)
       {
           success = false;
           se.printStackTrace();
           log("ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se);
           throw se;
       }


       // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
       AdaptorDAO.sendAllNotifications( VENDOR_NAME_UTAC, true, true, true );

       // To Close the Drop Out Lots
       AdaptorDAO.closeDropOutLots( VENDOR_NAME_UTAC );

        int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( UTACAdaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UTACAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UTACAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }
        // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
    }//end of execute method

    
     /** PARSES THE DETAILS BUMP/CP SHIP SHEET DATA INTO ARRAY LIST
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private ArrayList parseAssemblyWIPReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList assyWipList           = new ArrayList();
        String line                     = null;

        try
        {
            log( "\nFound " + fileName );
            log( "\nParsing the file for UTAC Assemby Wip Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for UTAC Assemby Wip Report..." );

            BufferedReader reader = new BufferedReader( new FileReader( fileName ) );
            reader.readLine();  // Read out <TimeStamp> Line

            while ( ( line = reader.readLine() ) != null  && line.length() > 0 )
            {
                UTACAssemblyWipStatus wipStatus = new UTACAssemblyWipStatus();

                line = line.replaceAll( "\"", "" );
                String[] token = line.split( "," );

                for (int x = 0; x < token.length; x++)
                {
                    if (token[x] != null && token[x].trim().length() > 0)
                    {
                        switch (x)
                        {
                            case 0:
                                    wipStatus.setTimeStamp(token[x].trim() );
                                    break;
                            case 1:
									wipStatus.setCustLotNumber(token[x].trim() );
                                    break;
                            case 2:
                                    wipStatus.setVendorLotNumber( token[x].trim() );
                                    break;
                            case 3:
                                    wipStatus.setParentLotNumber( token[x].trim() );
                                    break;
                            case 4:
                                    wipStatus.setCustPartInNumber( token[x].trim());
                                    break;
                            case 5:
                                    wipStatus.setCustPartOutNo( token[x].trim());
                                    break;
                            case 6:
                                    wipStatus.setPartId( token[x].trim() );
                                    break;
                            case 7:
                                    wipStatus.setLotType( token[x].trim());
                                    break;
                            case 8:
                                    wipStatus.setActualShipDate( token[x].trim());
                                    break;
                            case 9:
                                    wipStatus.setCommitShipDate ( token[x].trim());
                                    break;
                            case 10:
                                    wipStatus.setPriority( token[x].trim() );
                                    break;
                            case 11:
                                    wipStatus.setEqpId ( token[x].trim());
                                    break;
                            case 12:
                                    wipStatus.setCustomer( token[x].trim() );
                                    break;
                            case 13:
                                    wipStatus.setProdFamily( token[x].trim());
                                    break;
                            case 14:
                                    wipStatus.setDeviceName( token[x].trim());
                                    break;
                            case 15:
                                    wipStatus.setPakage( token[x].trim());
                                    break;
                            case 16:
                                    wipStatus.setCustDeviceName( token[x].trim());
                                    break;
                            case 17:
                                    wipStatus.setSupplierId( token[x].trim());
                                    break;
                            case 18:
                                    wipStatus.setReceiveDateIn( token[x].trim() );
                                    break;
                            case 19:
                                    wipStatus.setReceiveQty( token[x].trim());
                                    break;
                            case 20:
                                    wipStatus.setDBankDateIn( token[x].trim());  //P200 DBANK
                                    break;
                            case 21:
                                    wipStatus.setDBankQty( token[x].trim());
                                    break;
                            case 22:
                                    wipStatus.setPcc2DateIn(token[x].trim());
                                    break;
                            case 23:
                                    wipStatus.setPcc2Qty(token[x].trim());
                                    break;
                            case 24:
                                    wipStatus.setBkgdDateIn ( token[x].trim());
                                    break;
                            case 25:
                                    wipStatus.setBkgdQty ( token[x].trim());
                                    break;
                            case 26:
                                    wipStatus.setSawDateIn( token[x].trim() );
                                    break;
                            case 27:
                                    wipStatus.setSawQty( token[x].trim() );
                                    break;
                            case 28:
                                    wipStatus.setDBondDateIn( token[x].trim());
                                    break;
                            case 29:
                                    wipStatus.setDBondQty( token[x].trim());
                                    break;
                            case 30:
                                    wipStatus.setDacDateIn( token[x].trim());
                                    break;
                            case 31:
                                    wipStatus.setDacQty( token[x].trim());
                                    break;
                            case 32:
                                    wipStatus.setWBondDateIn( token[x].trim());
                                    break;
                            case 33:
                                    wipStatus.setWBondQty( token[x].trim());
                                    break;
                            case 34:
                                    wipStatus.setMoldDateIn( token[x].trim());
                                    break;
                            case 35:
                                    wipStatus.setMoldQty( token[x].trim());
                                    break;
                            case 36:
                                    wipStatus.setTopmDateIn( token[x].trim());
                                    break;
                            case 37:
                                    wipStatus.setTopmQty( token[x].trim());
                                    break;
                            case 38:
                                    wipStatus.setPmcDateIn( token[x].trim());
                                    break;
                            case 39 :
                                    wipStatus.setPmcQty( token[x].trim());
                                    break;
                            case 40:
                                    wipStatus.setTrimDateIn( token[x].trim());
                                    break;
                            case 41:
                                    wipStatus.setTrimQty( token[x].trim());
                                    break;
                            case 42:
                                    wipStatus.setFormDateIn(token[x].trim());
                                    break;
                            case 43:
                                    wipStatus.setFormQty(token[x].trim());
                                    break;
                            case 44:
                                    wipStatus.setT3DateIn ( token[x].trim());
                                    break;
                            case 45:
                                    wipStatus.setT3Qty ( token[x].trim());
                                    break;
                            case 46:
                                    wipStatus.setVmDateIn( token[x].trim() );
                                    break;
                            case 47:
                                    wipStatus.setVmQty( token[x].trim() );
                                    break;
                            case 48:
                                    wipStatus.setBakeDateIn( token[x].trim());
                                    break;
                            case 49:
                                    wipStatus.setBakeQty( token[x].trim());
                                    break;
                            case 50:
                                    wipStatus.setPtcDateIn( token[x].trim());
                                    break;
                            case 51:
                                    wipStatus.setPtcQty( token[x].trim());
                                    break;
                         }

                    }

                }
                if ( wipStatus.getDeviceName() != null && wipStatus.getDeviceName().length() > 0 )
                {
                    assyWipList.add( wipStatus );
                }
            }

            try
            {
                if ( reader != null )
                {
                   reader.close();
                }
            }
            catch(IOException ignore)
            {

            }

        }
        catch ( Exception ex )
        {
            System.out.println("ERROR!!!ERROR!!! While Parsing the UTAC Assembly Wip Sheet");
            ex.printStackTrace();
            throw ex;
        }

        return assyWipList;

    } // end of prase Assembly Wip Report method

    private void getAllAssemblyWipLotDetails(ArrayList assyWipList) throws Exception
    {
        if ( assyWipList == null || assyWipList.size() == 0 )
        {
            System.out.println("\n\n ********No data Found**********\n\n");
            return;
        }

        UTACAssemblyWipStatus wipStatus     = null;
        AssemblyLot           assemblyLot   = null;
        TestLot               testLot       = null;
        InventoryLot          dbinventoryLot= null;
        String partNumber                   = null;
        String lotNumber                    = null;
        String fabLotNumber                 = null;
        String lotCategory                  = null;
        try
        {
            Iterator utacWipIterator = assyWipList.iterator();

            while (utacWipIterator.hasNext())
            {

                wipStatus = (UTACAssemblyWipStatus) utacWipIterator.next();

                partNumber = wipStatus.getCustDeviceName();
                lotNumber = wipStatus.getVendorLotNumber();
                fabLotNumber = wipStatus.getCustLotNumber();

                if ( wipStatus.getLotType() != null && "EL".equalsIgnoreCase( wipStatus.getLotType() ) )
                 {
                     lotCategory = LOT_CATEGORY_TYPE_ENGG;
                 }
                 else
                 {
                     lotCategory = LOT_CATEGORY_TYPE_PROD;
                 }
                if( lotNumber != null && partNumber != null && fabLotNumber != null  )
                {
                    //DIE BANK LOTS
                    if (  wipStatus.getDBankQty() != null && wipStatus.getDBankQty().equalsIgnoreCase("0") == false   )
                    {
                        int bomPocessstep_id = NewAdaptorDAO.getBOMProcessStepId( partNumber , WIP_TYPE_ASSEMBLY );
                        
                        if ( bomPocessstep_id > 0 )
                        {
                            log ( "In BOM Die bank--> Assembly Bom" + lotNumber );
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get( partNumber + lotNumber + fabLotNumber );
                            if (assemblyLot == null)
                            {
                                assemblyLot = new AssemblyLot( VENDOR_NAME_UTAC );
                                m_assyWipDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                            }
                            assemblyLot.setLotNumber( lotNumber );
                            assemblyLot.setDeviceName( partNumber );
                            assemblyLot.setStatus(LOT_STATUS_RUNNING);
                            assemblyLot.setFabLotNumber( fabLotNumber );
                            assemblyLot.setPackageType( wipStatus.getPakage() );
                            assemblyLot.setCategory( lotCategory );
                            if ( wipStatus.getActualShipDate() != null && wipStatus.getActualShipDate().indexOf( " ") != -1 )
                            {
                                assemblyLot.setExpectedDateOut( wipStatus.getActualShipDate().substring(0, wipStatus.getActualShipDate().indexOf( " ") ) );
                            }
                            if ( wipStatus.getDBankDateIn() != null && wipStatus.getDBankDateIn().indexOf( " ") != -1 )
                            {
                                assemblyLot.setDateIn( wipStatus.getDBankDateIn().substring(0, wipStatus.getDBankDateIn().indexOf( " ") ) ) ;
                            }
                            assemblyLot.setQtyIn( wipStatus.getDBankQty() );
                            assemblyLot.setProcessType( WIP_TYPE_ASSEMBLY );

                        }
                        else
                        {
                             log ( "In Die bank" + lotNumber );

                             dbinventoryLot = (InventoryLot) m_dieBankDataMap.get( partNumber + lotNumber + fabLotNumber );
                             if ( dbinventoryLot == null )
                             {
                                 dbinventoryLot = new InventoryLot(VENDOR_NAME_UTAC, INVENTORY_TYPE_DIE_BANK );
                                 m_dieBankDataMap.put( partNumber + lotNumber + fabLotNumber, dbinventoryLot );
                             }

                             String expectedDateOut  = null;
                             String dateIn           = null;
                             int quantity = 0;
                            if (dbinventoryLot.getQtyIn() != null)
                            {
                                try
                                {
                                    quantity = Integer.parseInt(dbinventoryLot.getQtyIn());
                                }
                                catch (NumberFormatException ignore) {
                                }
                                try
                                {
                                    quantity += Integer.parseInt(wipStatus.getDBankQty());
                                }
                                catch (NumberFormatException nfe) {
                                }
                            }
                            else
                            {
                                try
                                {
                                    quantity = Integer.parseInt(wipStatus.getDBankQty());
                                }
                                catch (NumberFormatException ignore) {
                                }
                            }
                            
                            if ( quantity >0 )
                            {
                                dbinventoryLot.setStage( "P200-DBANK");
                                dbinventoryLot.setLotNumber( lotNumber );
                                dbinventoryLot.setPartNumber( partNumber );
                                dbinventoryLot.setStatus(LOT_STATUS_RUNNING);
                                dbinventoryLot.setFabLotNumber( fabLotNumber );
                                dbinventoryLot.setCategory( lotCategory );
                                if ( wipStatus.getActualShipDate() != null && wipStatus.getActualShipDate().indexOf( " ") != -1 )
                                {
                                    expectedDateOut = wipStatus.getActualShipDate().substring(0, wipStatus.getActualShipDate().indexOf( " ") );
                                }
                                if ( wipStatus.getDBankDateIn() != null && wipStatus.getDBankDateIn().indexOf( " ") != -1 )
                                {
                                    dateIn = wipStatus.getDBankDateIn().substring(0, wipStatus.getDBankDateIn().indexOf( " ") );
                                }
                                dbinventoryLot.setDateIn( dateIn );
                                dbinventoryLot.setQtyIn( quantity +"" );
                                dbinventoryLot.setExpectedDate( expectedDateOut );
                            }
                        }

                    }

                    //ASSEMBLY
                    if ( wipStatus.getPcc2Qty() != null && wipStatus.getPcc2Qty().equalsIgnoreCase("0") == false
                        || wipStatus.getBkgdQty() != null && wipStatus.getBkgdQty().equalsIgnoreCase("0") == false
                        || wipStatus.getSawQty() != null && wipStatus.getSawQty().equalsIgnoreCase("0") == false
                        || wipStatus.getDBondQty() != null && wipStatus.getDBondQty().equalsIgnoreCase("0") == false
                        || wipStatus.getDacQty() != null && wipStatus.getDacQty().equalsIgnoreCase("0") == false
                        || wipStatus.getWBondQty() != null && wipStatus.getWBondQty().equalsIgnoreCase("0") == false
                        || wipStatus.getMoldQty() != null && wipStatus.getMoldQty().equalsIgnoreCase("0") == false
                        || wipStatus.getTopmQty() != null && wipStatus.getTopmQty().equalsIgnoreCase("0") == false
                        || wipStatus.getPmcQty() != null && wipStatus.getPmcQty().equalsIgnoreCase("0") == false
                        || wipStatus.getTrimQty() != null && wipStatus.getTrimQty().equalsIgnoreCase("0") == false
                        || wipStatus.getFormQty() != null && wipStatus.getFormQty().equalsIgnoreCase("0") == false    )
                   {
                        log( "In Assembly" + lotNumber ) ;

                        assemblyLot = (AssemblyLot) m_assyWipDataMap.get( partNumber + lotNumber + fabLotNumber );
                        if (assemblyLot == null)
                        {
                            assemblyLot = new AssemblyLot( VENDOR_NAME_UTAC );
                            m_assyWipDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                        }

                        String expectedDateOut  = null;
                        String dateIn           = null;
                                                                        
                        // FOR SETTING THE QTY IN
                        if( wipStatus.getPcc2Qty() != null && wipStatus.getPcc2Qty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getPcc2Qty() );
                            assemblyLot.setStage("P400-PCC2");
                            if ( wipStatus.getPcc2DateIn() != null && wipStatus.getPcc2DateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getPcc2DateIn().substring(0, wipStatus.getPcc2DateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getBkgdQty() != null && wipStatus.getBkgdQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getBkgdQty() );
                            assemblyLot.setStage("A100-BKGD");
                            if ( wipStatus.getBkgdDateIn() != null && wipStatus.getBkgdDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getBkgdDateIn().substring(0, wipStatus.getBkgdDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getSawQty() != null && wipStatus.getSawQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getSawQty() );
                            assemblyLot.setStage("A300-SAW");
                            if ( wipStatus.getSawDateIn() != null && wipStatus.getSawDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getSawDateIn().substring(0, wipStatus.getSawDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getDBondQty() != null && wipStatus.getDBondQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getDBondQty() );
                            assemblyLot.setStage("A450-DBOND");
                            if ( wipStatus.getDBondDateIn() != null && wipStatus.getDBondDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getDBondDateIn().substring(0, wipStatus.getDBondDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getDacQty() != null && wipStatus.getDacQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getDacQty() );
                            assemblyLot.setStage("A480-DAC");
                            if ( wipStatus.getDacDateIn() != null && wipStatus.getDacDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getDacDateIn().substring(0, wipStatus.getDacDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getWBondQty() != null && wipStatus.getWBondQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getWBondQty() );
                            assemblyLot.setStage("A500-WBOND");
                            if ( wipStatus.getWBondDateIn() != null && wipStatus.getWBondDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getWBondDateIn().substring(0, wipStatus.getWBondDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getMoldQty() != null && wipStatus.getMoldQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getMoldQty() );
                            assemblyLot.setStage("A700-MOLD");
                            if ( wipStatus.getMoldDateIn() != null && wipStatus.getMoldDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getMoldDateIn().substring(0, wipStatus.getMoldDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getTopmQty() != null && wipStatus.getTopmQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getTopmQty() );
                            assemblyLot.setStage("F150-TOPM");
                            if ( wipStatus.getTopmDateIn() != null && wipStatus.getTopmDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getTopmDateIn().substring(0, wipStatus.getTopmDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getPmcQty() != null && wipStatus.getPmcQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getPmcQty() );
                            assemblyLot.setStage("F200-PMC");
                            if ( wipStatus.getPmcDateIn() != null && wipStatus.getPmcDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getPmcDateIn().substring(0, wipStatus.getPmcDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getTrimQty() != null && wipStatus.getTrimQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getTrimQty() );
                            assemblyLot.setStage("F400-TRIM");
                            if ( wipStatus.getTrimDateIn() != null && wipStatus.getTrimDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getTrimDateIn().substring(0, wipStatus.getTrimDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getFormQty() != null && wipStatus.getFormQty().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setQtyIn( wipStatus.getFormQty() );
                            assemblyLot.setStage("F500-FORM");
                            if ( wipStatus.getFormDateIn() != null && wipStatus.getFormDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getFormDateIn().substring(0, wipStatus.getFormDateIn().indexOf( " ") );
                            }
                        }

                        if (assemblyLot.getQtyIn() != null && assemblyLot.getQtyIn().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setLotNumber( lotNumber );
                            assemblyLot.setDeviceName( partNumber );
                            assemblyLot.setStatus(LOT_STATUS_RUNNING);
                            assemblyLot.setFabLotNumber( fabLotNumber );
                            assemblyLot.setPackageType( wipStatus.getPakage() );
                            assemblyLot.setCategory( lotCategory );
                            if ( wipStatus.getActualShipDate() != null && wipStatus.getActualShipDate().indexOf( " ") != -1 )
                            {
                                expectedDateOut = wipStatus.getActualShipDate().substring(0, wipStatus.getActualShipDate().indexOf( " ") );
                            }
                            assemblyLot.setDateIn( dateIn );
                            assemblyLot.setExpectedDateOut( expectedDateOut );
                            assemblyLot.setProcessType( WIP_TYPE_ASSEMBLY );
                        }
                    }

                    //TEST
                    if ( wipStatus.getT3Qty() != null && wipStatus.getT3Qty().equalsIgnoreCase("0") == false
                        || wipStatus.getVmQty() != null && wipStatus.getVmQty().equalsIgnoreCase("0") == false
                        || wipStatus.getBakeQty() != null && wipStatus.getBakeQty().equalsIgnoreCase("0") == false
                        || wipStatus.getPtcQty() != null && wipStatus.getPtcQty().equalsIgnoreCase("0") == false    )
                    {
                       log( "In Test " + lotNumber );
       
                        testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber);

                        if (testLot == null)
                        {
                            testLot = new TestLot();
                            m_ftDataMap.put( partNumber + lotNumber + fabLotNumber, testLot);
                        }

                        String expectedDateOut  = null;
                        String dateIn           = null;

                        if( wipStatus.getT3Qty() != null && wipStatus.getT3Qty().equalsIgnoreCase("0") == false )
                        {
                            testLot.setQtyIn( wipStatus.getT3Qty() );
                            testLot.setStage("T300-T3");
                            if ( wipStatus.getT3DateIn() != null && wipStatus.getT3DateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getT3DateIn().substring(0, wipStatus.getT3DateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getVmQty() != null && wipStatus.getVmQty().equalsIgnoreCase("0") == false )
                        {
                            testLot.setQtyIn( wipStatus.getVmQty() );
                            testLot.setStage("E220-VM");
                            if ( wipStatus.getVmDateIn() != null && wipStatus.getVmDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getVmDateIn().substring(0, wipStatus.getVmDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getBakeQty() != null && wipStatus.getBakeQty().equalsIgnoreCase("0") == false )
                        {
                            testLot.setQtyIn( wipStatus.getBakeQty() );
                            testLot.setStage("E330-BAKE");
                            if ( wipStatus.getBakeDateIn() != null && wipStatus.getBakeDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getBakeDateIn().substring(0, wipStatus.getBakeDateIn().indexOf( " ") );
                            }
                        }
                        else if( wipStatus.getPtcQty() != null && wipStatus.getPtcQty().equalsIgnoreCase("0") == false )
                        {
                            testLot.setQtyIn( wipStatus.getPtcQty() );
                            testLot.setStage("P600-PTC");
                            if ( wipStatus.getPtcDateIn() != null && wipStatus.getPtcDateIn().indexOf( " ") != -1 )
                            {
                                dateIn = wipStatus.getPtcDateIn().substring(0, wipStatus.getPtcDateIn().indexOf( " ") );
                            }
                        }

                        if (testLot.getQtyIn() != null && testLot.getQtyIn().equalsIgnoreCase("0") == false )
                        {
                            testLot.setLotNumber( lotNumber );
                            testLot.setPartNumber( partNumber );
                            testLot.setVendorName( VENDOR_NAME_UTAC );
                            testLot.setStatus( LOT_STATUS_RUNNING );
                            testLot.setPsType( WIP_TEST_TYPE_FT );
                            testLot.setFabLotNumber( fabLotNumber );
                            testLot.setPackageType( wipStatus.getPakage() );
                            testLot.setCategory( lotCategory );

                            if ( wipStatus.getActualShipDate() != null && wipStatus.getActualShipDate().indexOf( " ") != -1 )
                            {
                                expectedDateOut = wipStatus.getActualShipDate().substring(0, wipStatus.getActualShipDate().indexOf( " ") );
                            }
                            testLot.setDateIn( dateIn );
                            testLot.setExpecetedDate( expectedDateOut );
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            log("ERROR!ERROR! While parsing the UTAC Wip Details" + e);
            System.out.println("ERROR!ERROR! While parsing the UTAC Wip Details" + e);
        }
    }

    private static void log( String message )
    {

        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            synchronized ( m_parserLogger )
            {
                m_parserLogger.logDebug( message + LINE_SEPARATOR );
            }
        }
        else
        {
            System.out.println( message );
        }
    }

    private void printVersionInfo()
    {
       String info = "\n\n+----------- Running UTAC Data Parser on " + new java.util.Date( System.currentTimeMillis() ) + " -------------+\n" +
                "\n+---------------------------------------------------------------+\n" +
                "+                                                                \n" +
                "+            UTAC Data Parser - Beta                             \n" +
                "+                                                                \n" +
                "+    Copy Right (C) 2007 - ScalarSoft Inc., All rights reserved.  \n" +
                "+                                                                \n" +
                "+---------------------------------------------------------------+\n" ;
       System.out.println( info );
       log( info );
    } //end of printVersionInfo
}
